﻿using Cofe.Core.Property;
using Cofe.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Cofe.Core.Implements
{
    /// <summary>
    /// Represent an entry's metadata or resources.
    /// </summary>
    public class ResourceList : IXmlSerializable
    {
        #region Constructor

        public ResourceList(IPropertyHost ph, IPropertyFilter[] propertyFilters)
        {
            _ph = ph;
            _propertyFilters = propertyFilters;
        }
        
        #endregion

        #region Methods

        private async Task fetchAsync()
        {
            _metadataDic.Clear();
            _resourceList.Clear();

            var propList = await _ph.Behaviors.GetSupportedPropertyAsync(PropertyRetrivalOptions.Default, MatchMode.Any, _propertyFilters);

            foreach (var p in propList)
            {
                if (DecoratorExtension.IsMetadata(p))
                    _metadataDic.Add(CofeServices.PropertyDictionary.GetPropertyString(p),
                        (await _ph.Behaviors.GetPropertyAsync(p)).Value);
                else if (DecoratorExtension.IsWebResource(p))
                    _resourceList.Add(CofeServices.PropertyDictionary.GetPropertyString(p));
            }
        }


        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            throw new NotImplementedException();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Data

        private IPropertyHost _ph;
        private IPropertyFilter[] _propertyFilters;
        private Dictionary<string, object> _metadataDic = new Dictionary<string, object>();
        private List<string> _resourceList = new List<string>();
        
        #endregion

        #region Public Properties
        
        #endregion


     
    }

    //public class ResourceList : IResourceList
    //{
    //    public ResourceList(object parentResource, IEnumerable<object> resources)
    //    {
    //        ParentResource = parentResource;
    //        _resources = new List<object>(resources);
    //    }

    //    public ResourceList(IPropertyHost propertyHost, object parentResource,
    //        PropertyRetrivalOptions options = PropertyRetrivalOptions.NotApplied)
    //    {
    //        ParentResource = parentResource;

    //        var ra = AttributeUtils<ResourceAttribute>.FindAttribute(parentResource);
    //        if (ra == null && ra.MajorProperty)
    //            throw new ArgumentException("Parent resource is not a major resource.");
    //        var rla = AttributeUtils<ResourceListAttribute>.FindAttribute(parentResource);
    //        if (rla == null)
    //            throw new ArgumentException("Parent resource do not have ResourceListAttribute tagged.");
    //        _resources = new List<object>(propertyHost.Behaviors.GetSupportedPropertyAsync(options, rla.MatchMode, rla.Filters).Result);
    //    }

    //    private List<object> _resources;

    //    public object ParentResource { get; private set; }

    //    public IEnumerable<object> Resources { get { return _resources; } }

    //}
}
